#include "worker.h"
#include "MySQL.h"
#include "taskQueue.h"
#include "user.h"
#include <mysql/mysql.h>
void *threadFunc(void *argv){                                                     
    threadPool_t *pThreadPool=(threadPool_t *)argv;                               
    while(1){                                                                     
        while(!pThreadPool->exitFlag&&pThreadPool->taskQueue.queueSize<=0){       
            pthread_cond_wait(&pThreadPool->cond,&pThreadPool->mutex);            
        }                                                                         
        if(pThreadPool->exitFlag){                                                
            pthread_mutex_unlock(&pThreadPool->mutex);                            
            pthread_exit(NULL);                                                   
        }                                             
        MYSQL *mysql=NULL;
        MySQLCon(&mysql,&pThreadPool->configUsrInfo);
        usrInfo_t usrInfo=pThreadPool->taskQueue.pFront->usrInfo;                 
        deQueue(&pThreadPool->taskQueue);                                         
        pthread_mutex_unlock(&pThreadPool->mutex);    
        user(&usrInfo,mysql);
        while(1){
            char buf[1024]={0};
            train_t train;
            int ret=recv(usrInfo.netFd,&train,sizeof(train),MSG_WAITALL);
            if(ret==0){
                printf("客户端退出\n");
                close(usrInfo.netFd);
                break;
            }
            commandExcute(&usrInfo,&train,pThreadPool,mysql);
        }
    }
}

int makeWorker(threadPool_t *pThreadPool){
    for(int i=0;i<pThreadPool->tidArr.workerNum;++i){
        pthread_create(&pThreadPool->tidArr.pWorker[i],NULL,threadFunc,pThreadPool);
    }
    return 0;
}
